/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.yonsei.iwb.smarttv.ner;

/**
 *
 * @author DK
 */
import java.io.IOException;
import java.util.Vector;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.FieldCache;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.function.DocValues;
import org.apache.lucene.search.function.FloatFieldSource;

public class OrderCheckingFunction extends FloatFieldSource {

    private Vector<String> user_query = new Vector<String>();
    private String field;

    public OrderCheckingFunction(String field2, Vector userquery) {
        super(field2);
        field = field2;
        user_query = userquery;
    }

    @Override
    public DocValues getCachedFieldValues(FieldCache cache, String field, IndexReader reader) throws IOException {
        IndexSearcher searcher = new IndexSearcher(reader.directory());
        return new OrderDocValues(user_query, searcher);
    }

    /**
     * Query와 Index 사이의 Order Consistency Check
     */
    public class OrderDocValues extends DocValues {

        private Vector<String> user_query = new Vector<String>();
        IndexSearcher searcher;

        public OrderDocValues(Vector userquery, IndexSearcher ordersearcher) {
            super();
            user_query = userquery;
            searcher = ordersearcher;
        }

        @Override
        public float floatVal(int doc) {
            float order_consistency = 1;
            float overlap = 0;
            try {
                Document d = searcher.doc(doc);
                String indexValue = d.get(field);
                String[] s = indexValue.split(" ");
                Vector<String> iv = new Vector<String>();
                for (int n = 0; n < s.length; n++) {
                    iv.add(s[n]);
                }
                for (int i = 0; i < user_query.size(); i++) {
                    if (user_query.size() == 1) {
                    } else {
                        //user_query combination
                        Vector<Vector<String>> queryCombination = new Vector<Vector<String>>();
                        for (int k = 0; k < user_query.size(); k++) {
                            Vector<String> subQueryComb;
                            for (int j = k + 1; j < user_query.size(); j++) {
                                subQueryComb = new Vector<String>();
                                subQueryComb.add(user_query.get(k));
                                subQueryComb.add(user_query.get(j));
                                queryCombination.add(subQueryComb);
                            }
                        }

                        //check the order
                        for (int m = 0; m < queryCombination.size(); m++) {
                            Vector<String> subQueryCombination = queryCombination.get(m);
                            String predecessor = subQueryCombination.get(0);
                            String successor = subQueryCombination.get(1);
                            if (iv.contains(predecessor) && iv.contains(successor)) {
                                if (iv.indexOf(predecessor) < iv.indexOf(successor)) {
                                    overlap += 1;
                                }
                            }
                        }
                        if (overlap == 0) {
                            order_consistency = (float) 0.00001;
                        } else {
                            order_consistency = overlap / queryCombination.size();
                        }
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return order_consistency;
        }

        @Override
        public String toString(int doc) {
            return super.toString();
        }
    }
}
